package com.zuo.demo01.controller.admin;


import com.zuo.demo01.config.Constants;
import com.zuo.demo01.pojo.Blog;
import com.zuo.demo01.pojo.Catagory;
import com.zuo.demo01.pojo.Config;
import com.zuo.demo01.pojo.User;
import com.zuo.demo01.service.BlogService;
import com.zuo.demo01.service.CatagoryService;
import com.zuo.demo01.service.ConfigService;
import com.zuo.demo01.service.UserService;
import com.zuo.demo01.utils.FileUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private ConfigService configService;

    @Autowired
    private CatagoryService catagoryService;

    @Autowired
    private BlogService blogService;

    @RequestMapping("/out")
    public String sign_out(HttpSession session){


        session.removeAttribute("loginUser");
        session.removeAttribute("pic");
        session.removeAttribute("nickname");
        session.removeAttribute("selfemail");

        List<Blog> blogs = blogService.getAll();
        File files = new File(Constants.FILE_IMGS_BLOGS_CONTENT);
        if(files.exists()){
            for (File file :
                    files.listFiles()) {
                int i=0;
                for (Blog blog :
                        blogs) {
                    if(blog.getUrl().equals(file.getName())){//如果数据库中存有文件夹名字就不删除
                        break;//直接跳过
                    }else{
                        i++;//和数据库中不相等的次数
                    }
                    if(i==blogs.size()){//和数据库中全部不相等，就可以删除文件夹了
                        for (File cont :
                                file.listFiles()) {
                            cont.delete();
                        }
                        file.delete();
                    }
                }
            }
        }
        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return "redirect:/myLogin";
    }

    @RequestMapping({"/","/index"})
    public String index(){
        return "admin/index";
    }


    @RequestMapping("/edit")
    public String edit(Model model){
        List<Catagory> categories = catagoryService.findAll();
        model.addAttribute("categories",categories);

        model.addAttribute("token",0);

        String dir = FileUtil.randomName();
        File createFile = new File(Constants.FILE_IMGS_BLOGS_CONTENT+"/"+dir);
        if(!createFile.exists()){
            createFile.mkdir();//下边的流不能创建文件夹，只能在此创建文件夹
        }
        model.addAttribute("dir",dir);

        return "admin/edit";
    }


    @RequestMapping("/blogs")
    public String blogs(){
        return "admin/blogs";
    }

    @RequestMapping("/comments")
    public String comments(){
        return "admin/comments";
    }

    @RequestMapping("/catagory")
    public String catagory(){
        return "admin/catagory";
    }

    @RequestMapping("/tags")
    public String tags(){
        return "admin/tags";
    }

    @RequestMapping("/links")
    public String links(){
        return "admin/links";
    }

    @RequestMapping("/config")
    public String config(Model model){
        Map<String,String> configs = configService.findAll();
        model.addAttribute("configs",configs);
        return "admin/config";
    }

    @RequestMapping("/change")
    public String change(Model model,HttpSession session){
        User loginUser = (User)session.getAttribute("loginUser");
        //改动的时候把session再改一下(不用了，修改成功直接会跳到登录界面)
        model.addAttribute("loginUser",loginUser);

        return "admin/change";
    }

}
